#include "Win32Lib.h"
#include "HookSettingEntry.h"


#pragma once

class HookRecordList;
class HeapCreateWrapper;



class HookEngine
{
public:
	HookEngine();
	BOOL Start(HookSettingEntry * pHookSettingEntry);
	BOOL Stop(HookSettingEntry * pHookSettingEntry);
	~HookEngine();

	static BOOL SuspendAllThreads();
	static BOOL ResumeAllThreads();
private:
	enum HookOperation
	{
		HookAdd,
		HookRemove
	};

	auto_ptr<HeapCreateWrapper> pHCWrapper;

	BYTE GetOverwrittenAPIEntryPointLength(BYTE * pAPIAddr);
	DWORD SetMemoryBlockProtection(LPCVOID pMemoryAddress, DWORD dwNewProtection);
	BOOL OverwriteAPIEntryPoint(LPVOID pAPIEntryAddress, LPCVOID pOverwrittenInstructionBuf, SIZE_T nOverwrittenInstructionBufLength );

	BOOL IsEipInCodeRange(DWORD_PTR eip, DWORD_PTR dwpStartAddress, DWORD dwpCodeRangeLength);
	
	BOOL UpdateAllThreads(DWORD_PTR dwpAPIAddress, HookOperation operation);
	BOOL IsHookInUse(DWORD_PTR dwpAPIAddress);
};

void FreeTLSMemory();
